var rRouter		= require('./rRouter');

exports.run = function(req, res){

	function _parseUrl() {
		return req.url;
	}

	function _parseReferer() {
		return req.headers['referrer'];
	}

	function _parseMethod() {
		return req.method;
	}

	function _runRoute(data) {
		var reqData = {
			url : _parseUrl(),
			method : _parseMethod(),
			referer : _parseReferer(),
			data : data
		};
		rRouter.run(reqData, res);
	}

	var data = '';

	switch (_parseMethod()) {
		case 'GET':
		case 'DELETE':
			_runRoute(data);
			break;
		case 'POST':
		case 'PUT':
			req.setEncoding(_rConfig.charset);
			req.addListener("data", function(chunk) {
			  data += chunk;
			});
			req.addListener("end", function(){
				_runRoute(data);
			});
			break;
	}

}